<template>
  <div class="system-user-dialog-container">
    <el-dialog :close-on-click-modal="false" :title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
               draggable v-model="visible">
      <el-form :model="dataForm" :rules="dataRules" label-width="90px" ref="dataFormRef" v-loading="loading">
        <el-row :gutter="20">
          <el-col :span="24" class="mb20">
            <el-form-item :label="$t('village.name')" label-width="150" prop="name">
              <el-input placeholder="请输入村名称" v-model="dataForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item :label="$t('village.villageSort')" label-width="150" prop="villageSort">
              <el-input placeholder="排序" v-model="dataForm.villageSort"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item :label="$t('village.doctor')" label-width="150" prop="doctorId">
              <el-select clearable placeholder="请选择医生" v-model="dataForm.doctorId">
                <el-option :key="item.userId" :label="item.name" :value="item.userId"
                           v-for="item in doctorList"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24" class="mb20">
            <el-form-item label="专病医生" label-width="150" prop="zbDoctor">
              <el-select clearable v-model="dataForm.zbDoctor" placeholder="请输入专病医生">
                <el-option :key="item.userId" :label="item.name" :value="item.userId"
                           v-for="item in ZBDoctorList"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24" class="mb20">
            <el-form-item label="专病医生助理" label-width="150" prop="zlId">
              <el-select clearable v-model="dataForm.zlId" placeholder="请输入助理">
                <el-option :key="item.userId" :label="item.name" :value="item.userId"
                           v-for="item in ZLDoctorList"/>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="24" class="mb20">
            <el-form-item label="状态" label-width="150" prop="villageStatus">
              <el-radio-group v-model="dataForm.villageStatus">
                <el-radio :key="index" :label="item.value" border v-for="(item,index) in villageStatus">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12" class="mb20">
            <el-form-item :label="$t('village.lockFlag')" prop="lockFlag">
              <el-radio-group v-model="dataForm.lockFlag">
                <el-radio :key="index" :label="item.value" border v-for="(item, index) in lock_flag">{{ item.label }} </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
					<el-button @click="onSubmit" type="primary" :disabled="loading">{{
              $t('common.confirmButtonText')
            }}</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="systemUserDialog" setup>
// import { addObj, getObj, putObj, validatePhone, validateUsername } from '/@/api/admin/user';

import {getDoctorList, addVillage, editVillage, getAllZbDoctor, getAllZbAideDoctor} from '/@/api/information/village';

// import { useDict } from '/@/hooks/dict';
import {useI18n} from 'vue-i18n';
import {useMessage} from '/@/hooks/message';
// import { rule } from '/@/utils/validate';

const {t} = useI18n();

// 定义刷新表格emit
const emit = defineEmits(['refresh']);
// @ts-ignore
// const { lock_flag } = useDict('lock_flag');

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);

const doctorList = ref<any[]>([]);
const ZLDoctorList = ref<any[]>([]);
const loading = ref(false);
const currentId = ref(null);

const villageStatus = ref([
  {label: '启用', value: "1"},
  {label: '禁用', value: "0"},
]);

const dataForm = reactive({
  id: null,
  name: '',
  villageSort: '',
  doctorId: null,
  villageStatus: "1",
  zbDoctor: '',
  zlId: '',
});

const ZBDoctorList = ref<any[]>([]);

const dataRules = ref({
  name: [{required: true, message: '村名不能为空', trigger: 'blur'}],
  villageSort: [{required: true, message: '排序不能为空', trigger: 'blur'}],
  doctorId: [{required: true, message: '请选择医生', trigger: 'blur'}],
});

// 打开弹窗
const openDialog = async (data: object) => {
  visible.value = true;
  // 重置表单数据
  await nextTick(() => {
    if (data) {
      const {name, villageSort, doctorId, id, villageStatus,zbDoctor,zlId}: any = data;
      dataFormRef.value?.resetFields();
      currentId.value = id;
      dataForm.id = id;
      dataForm.name = name;
      dataForm.villageSort = villageSort;
      dataForm.doctorId = doctorId;
      dataForm.villageStatus = villageStatus;
      dataForm.zbDoctor = zbDoctor;
      dataForm.zlId = zlId;
    } else {
      dataFormRef.value?.resetFields();
      dataForm.doctorId = '';
      // console.log(dataForm.doctorId);
    }
  });

  // 修改获取用户信息

  // 加载使用的数据

  getdoctorList();
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    if (currentId.value) {
      loading.value = true;
      await editVillage(dataForm);
      useMessage().success(t('common.editSuccessText'));
      visible.value = false; // 关闭弹窗
      emit('refresh');
    } else {
      loading.value = true;
      await addVillage(dataForm);
      useMessage().success(t('common.addSuccessText'));
      visible.value = false; // 关闭弹窗
      emit('refresh');
    }
  } catch (error: any) {
    useMessage().error(error.msg);
  } finally {
    loading.value = false;
  }
};

// 岗位数据
const getdoctorList = () => {
  getDoctorList().then((res) => {
    doctorList.value = res.data;
  });

  getAllZbDoctor().then((res) => {
    ZBDoctorList.value = res.data;
  });

  getAllZbAideDoctor().then((res) => {
    ZLDoctorList.value = res.data;
  });
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
